import {Injectable} from "@angular/core";
import {HttpClient, HttpParams} from '@angular/common/http';

@Injectable()
export class TrainingService {
  userServeUrl = 'https://accountserver.lessonplan.cn';//用户相关的Url地址
  rosterServeUrl = 'https://rosterserver.lessonplan.cn'; //班级相关接口Url地址
  trainingServeUrl = 'https://creativitytrainingserver.lessonplan.cn';//创造力相关接口地址
  paperGuid = '5c21329e-6ff1-4ea5-abbd-532b9c506ded';//威廉斯创造试卷ID
  stuServeUrl='https://clientaccountserver.lessonplan.cn'; /*学生用户API地址*/
  codeServerUrl = 'https://codeserver.lessonplan.cn/api/';/*进入码接口*/
  constructor(private http: HttpClient,) {
  }

  /*根据用户Guid获取用户详细信息*/
  getUserInfo(userGuid: string): Promise<any> {
    return this.http.get(this.userServeUrl + '/open/getThisUser', {
      params: new HttpParams().set('PK_UserGuid', userGuid)
    }).toPromise().then(res => {
      return res;
    })
  }

  /*根据用户Guid获取用户班级列表*/
  getRosterList(userGuid, Valid, State): Promise<any> {
    return this.http.get(this.rosterServeUrl + '/class/getClassList/' + userGuid, {
      params: new HttpParams().set('Valid', Valid).set('State', State)
    }).toPromise().then(res => {
      return res;
    })
  }

  /*创建训练*/
  createTraining(training): Promise<any> {
    let data = {
      FK_PaperGuid: training.FK_PaperGuid,
      TrainingName: training.name,
      Description: training.desc,
      Creater: training.creater,
      InviteID: training.inviteCode,
      FK_ClassGuid: training.classGuid
    };
    return this.http.post(this.trainingServeUrl + '/training', data).toPromise().then(res => {
      return res;
    })
  }

  /*获取根据试卷Guid和创建者的Guid获取训练status:1代表获取未删除的*/
  getTrainingList(userGuid, status): Promise<any> {
    return this.http.get(this.trainingServeUrl + '/training/paper', {
      params: new HttpParams().set('id', this.paperGuid).set('status', status).set('Creater', userGuid)
    }).toPromise().then(res => {
      return res;
    })
  }

  /*根据班级Guid获取班级信息*/
  getClassInfo(classGuid: string): Promise<any> {
    return this.http.get(this.rosterServeUrl + '/class/private/', {
      params: new HttpParams().set('classListString', '"' + classGuid + '"')
    }).toPromise().then(res => {
      return res;
    })
  }

  /*根据Guid删除训练*/
  deleteTraining(trainingId: string): Promise<any> {
    return this.http.delete(this.trainingServeUrl + '/training', {
      params: new HttpParams().set('id', trainingId)
    }).toPromise().then(res => {
      return res;
    })
  }

  /*恢复删除的训练*/
  recoverTraining(trainingId: string): Promise<any> {
    return this.http.get(this.trainingServeUrl + '/training/recover', {
      params: new HttpParams().set('id', trainingId)
    }).toPromise().then(res => {
      return res;
    })
  }

  /*修改训练根据GUID*/
  modifyTrainingById(training): Promise<any> {
    let data = {
      id: training.id,
      TrainingName: training.name,
      Description: training.desc,
      Creater: training.creater,
      FK_ClassGuid: training.classGuid
    };
    return this.http.put(this.trainingServeUrl + '/training', data).toPromise().then(res => {
      return res;
    })
  }
  /*根据用户id和班级id获取和试卷ID获取测试*/
  getTrainingByClass(userID,classId=null,status:number=1):Promise<any>{
     return this.http.get(this.trainingServeUrl + '/training/class',{
       params:new HttpParams().set('userID',userID).set('classID',classId).set('paperID',this.paperGuid).set('status',status.toString())
     }).toPromise().then(res=>{
        return res;
     })
  }
/*根据训练Guid获取训练*/
  getTrainingByID(trainingID):Promise<any>{
    return this.http.get(this.trainingServeUrl+'/training/id',{
      params:new HttpParams().set('id',trainingID)
    }).toPromise().then(res=>{
      return res;
    })
  }
/*根据训练Guid来获取已经完成的答题卡*/
getAnswerCardByTrainingId(trainingID):Promise<any>{
   return this.http.get(this.trainingServeUrl+'/anwser/training',{
     params:new HttpParams().set('id',trainingID)
   }).toPromise().then(res=>{
     return res;
   })
}
/*根据学生Guid获取学生个人信息*/
  getStuUserInfo(userGuid:string):Promise<any>{
    return this.http.get(this.stuServeUrl+'/user/usermsg/'+userGuid).toPromise().then(res=>{
      return res ;
    })
  }
  /*创建邀请码*/
  createCode(data:any):Promise<any>{
    return this.http.post(this.codeServerUrl+'create',data).toPromise().then(res=>{
      return res;
    })
  }
  /*根据训练guid查码*/
  getCodeBytrainingId(trainingId:string):Promise<any>{
    return this.http.get(this.codeServerUrl+'reverse/'+trainingId).toPromise().then(res=>{
      return res;
    })
  }
  /*删除邀请码*/
  deleteInviteCode(codeID:string):Promise<any>{
    return this.http.put(this.codeServerUrl+'remove/'+codeID,'').toPromise().then(res=>{
      return res;
    })
  }
  /*修改邀请码标题*/
  modifyInviteCodeTitle(Title:string,InviteCodeId:string):Promise<any>{
    return this.http.put(this.codeServerUrl+'rename/'+InviteCodeId,Title).toPromise().then(res=>{
      return res;
    })
  }
}



